﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Base
{
    public partial class frmhyfl : Form
    {
        public frmhyfl()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void frmhyfl_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "SELECT [cid] as 序号 ,[cname] as 名称  ,[cdiscount] as 折扣,[cmoney] as 办卡金额,积分方式,优惠方式,case 储值 when 1 then '是' when 0 then '否' end as 储值,case 计次 when 1 then '是' when 0 then '否' end as 计次,积分率 FROM [card]";
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            chuanzhi.ifup = 0;
            frmhyfladd add = new frmhyfladd();
            add.ShowDialog();
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "SELECT [cid] as 序号 ,[cname] as 名称  ,[cdiscount] as 折扣,[cmoney] as 办卡金额,积分方式,优惠方式,case 储值 when 1 then '是' when 0 then '否' end as 储值,case 计次 when 1 then '是' when 0 then '否' end as 计次,积分率 FROM [card]";
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
            chuanzhi cz = new chuanzhi();
            cz.rizhi("新增会员分类");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                chuanzhi.ifup = 1;
                chuanzhi.hyflname = dataGridView1.SelectedCells[1].Value.ToString();
                chuanzhi.hyzl = dataGridView1.SelectedCells[2].Value.ToString();

                chuanzhi.hyfl = int.Parse(dataGridView1.SelectedCells[0].Value.ToString());
                chuanzhi.bankajine = dataGridView1.SelectedCells[3].Value.ToString();
                chuanzhi.yhfs= dataGridView1.SelectedCells[5].Value.ToString();
                chuanzhi.jffs= dataGridView1.SelectedCells[4].Value.ToString();
                chuanzhi.jfl=decimal.Parse( dataGridView1.SelectedCells[8].Value.ToString());

                if (dataGridView1.SelectedCells[6].Value.ToString() == "是")
                {
                    chuanzhi.ifcz = 1;
                }
                else
                {
                    chuanzhi.ifcz = 0;
                }
                if (dataGridView1.SelectedCells[7].Value.ToString() == "是")
                {
                    chuanzhi.ifjc = 1;
                }
                else
                {
                    chuanzhi.ifjc = 0;
                }

                frmhyfladd add = new frmhyfladd();
                add.ShowDialog();
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                string sql = "SELECT [cid] as 序号 ,[cname] as 名称  ,[cdiscount] as 折扣,[cmoney] as 办卡金额,积分方式,优惠方式,case 储值 when 1 then '是' when 0 then '否' end as 储值,case 计次 when 1 then '是' when 0 then '否' end as 计次,积分率 FROM [card]";
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView1.DataSource = dt;
                conn.Close();
                chuanzhi.ifup = 0;
            }
            catch { }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //判断是否有卡已经使用要删除的卡分类
            SqlConnection conn1 = new SqlConnection(constr);
            conn1.Open();
            string sql =string.Format( "select * from member where mcid={0}", dataGridView1.SelectedCells[0].Value.ToString());
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn1);
            DataTable dt1 = new DataTable();
            sda.Fill(dt1);
            conn1.Close();
            if (dt1.Rows.Count > 0)
            {
                MessageBox.Show("此分类已经被使用不可删除");
            }
            else
            {
                SqlConnection conn = new SqlConnection(constr);
                string sqldel = string.Format("DELETE FROM [card] WHERE cid={0}", dataGridView1.SelectedCells[0].Value.ToString());
                conn.Open();
                SqlCommand comm = new SqlCommand(sqldel, conn);
                int row = comm.ExecuteNonQuery();
                if (row > 0)
                {
                    MessageBox.Show("删除成功");
                    jz();
                }
            }
           // MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());
        }
        public void jz()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "SELECT [cid] as 序号 ,[cname] as 名称  ,[cdiscount] as 折扣,[cmoney] as 办卡金额,积分方式,优惠方式,case 储值 when 1 then '是' when 0 then '否' end as 储值,case 计次 when 1 then '是' when 0 then '否' end as 计次,积分率 FROM [card]";
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
        }
    }
}
